import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        contactsCards: [
            {
                name: "Alex",
                email: "alex@foo.bar",
                phone: "503-555-1212",
                fingerprint: "b1:92:b2:8e:f6:e2:ab:64:29:5b:20:d3:5c:c0:f4:35:b2:9d:6d:69:f6:8b:4c:fb:47:34:17:9b:b2:00:d0:06"
            },
            {
                name: "Billie",
                email: "billie@foo.bar",
                phone: "206-555-1212",
                fingerprint: "2f:d1:8f:ed:8f:a7:ca:e2:50:90:d9:e2:86:06:35:d2:81:cb:87:72:bb:2f:f3:44:5a:59:60:c0:c8:86:9d:7e"
            },
            {
                name: "Carol",
                email: "carol@foo.bar",
                phone: "971-555-1212",
                fingerprint: "7b:ae:11:ea:4b:83:a1:d0:56:62:73:dc:74:f0:14:6e:33:8a:3b:0b:53:85:b7:08:16:62:cf:b3:4f:56:d5:ed"
            },
        ],
        walletCards: [
            {
                name: "Drew",
                email: "drew@foo.bar",
                phone: "360-555-1212",
                fingerprint: "1f:42:61:c6:9f:5b:95:d7:56:c6:53:ca:26:ae:96:d9:08:d4:da:aa:61:7e:4e:83:45:af:ba:d1:a0:81:ec:41"
            },
            {
                name: "de2005",
                email: "de2005@foo.bar",
                phone: "",
                fingerprint: "d1:01:84:76:58:a7:cb:78:dc:bf:82:80:b0:cc:8a:ff:e2:64:60:5b:0f:00:1a:0c:7b:16:61:ab:14:b6:70:21"
            },
            {
                name: "Drew Jansbarn",
                email: "drew.jansbarn@foo.bar",
                phone: "800-555-1212",
                fingerprint: "e7:b8:37:31:d0:c6:b1:85:31:2d:08:e6:6f:7d:2d:b9:28:88:fa:60:5e:90:47:81:ba:26:bd:b4:91:d0:ed:03"
            },
        ],
        ssiKeystore: [
            {
                fingerprint: "1f:42:61:c6:9f:5b:95:d7:56:c6:53:ca:26:ae:96:d9:08:d4:da:aa:61:7e:4e:83:45:af:ba:d1:a0:81:ec:41",
                pubKey: "-----BEGIN PUBLIC KEY-----\nMFowDQYJKoZIhvcNAQEBBQADSQAwRgJBAM0PD8UbtbWvV1TNc1OxcN8F0630rnCf\nu7cFDcvFWxF8nizNmySM45iuxfL1UDhHIw+NK3W9XLf3tWAJsFog/vECAQM=\n-----END PUBLIC KEY-----\n",
                pvtKey: "-----BEGIN PRIVATE KEY-----\nMIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEAzQ8PxRu1ta9XVM1z\nU7Fw3wXTrfSucJ+7twUNy8VbEXyeLM2bJIzjmK7F8vVQOEcjD40rdb1ct/e1YAmw\nWiD+8QIBAwJBAIi0tS4SeSPKOjiI940g9eoD4nP4dEsVJ89Ys90uPLZR4R/QiTHn\n7InvM94t8UUCiUS8GVDJ9cbL5YaHI+1OHisCIQD8/X1s71ox00W9NAmtYKWYLzAE\ndQ4MfU8K6IEm853xCQIhAM9/l2BqVs72gjrxprjwHbz5QwEHgF+QdtItvdOCjeCp\nAiEAqKj+SJ+RdozZKM1byOsZEB91WE4JXajfXJsAxKJpS1sCIQCKVQ+VnDnfTwF8\noRnQoBPTUNdWBQA/taSMHn6NAbPrGwIhAJiPFbMV0/fS0QnkCi5SbeutgQvt/5kl\n/ce8/2hn8Quh\n-----END PRIVATE KEY-----\n"
            },
            {
                fingerprint: "d1:01:84:76:58:a7:cb:78:dc:bf:82:80:b0:cc:8a:ff:e2:64:60:5b:0f:00:1a:0c:7b:16:61:ab:14:b6:70:21",
                pubKey: "-----BEGIN PUBLIC KEY-----\nMFowDQYJKoZIhvcNAQEBBQADSQAwRgJBANvFBllgZ1c8RN3s7YGscjhrYOvFYM5t\nRvgiSIpJuP2d5PvW2YgdahZibfI6x+Z/bf2Fr0VtzNvQaX4Wz7fUEXkCAQM=\n-----END PUBLIC KEY-----\n",
                pvtKey: "-----BEGIN PRIVATE KEY-----\nMIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEA28UGWWBnVzxE3ezt\ngaxyOGtg68Vgzm1G+CJIikm4/Z3k+9bZiB1qFmJt8jrH5n9t/YWvRW3M29BpfhbP\nt9QReQIBAwJBAJKDWZDq7499gz6d86vITCWc60fY6zRI2frBhbGGe1O9XGOFJj4v\nYbsLl78gWqwd1LquxpvDIa08PBIvHXOvNEMCIQDu6+l7Nl72OjTfXY3ld+3+TWCk\n58vFwj3ocA4jnyPhSwIhAOt6paT0d2FDnCr1/FpsZLCYHuBz/VSVuCbywf/rKWHL\nAiEAn0fw/M7p+XwjP5Oz7k/z/t5Abe/dLoF+mvVewmoX64cCIQCc/G5t+E+WLRLH\nTqg8SEMgZWnq9/44Y9AZ9yv/8huWhwIhAMPfAlcKVSwzBlYsG12Z8LId9CYygAFC\n8J/a31IAbI61\n-----END PRIVATE KEY-----\n"
            },
            {
                fingerprint: "e7:b8:37:31:d0:c6:b1:85:31:2d:08:e6:6f:7d:2d:b9:28:88:fa:60:5e:90:47:81:ba:26:bd:b4:91:d0:ed:03",
                pubKey: "-----BEGIN PUBLIC KEY-----\nMFowDQYJKoZIhvcNAQEBBQADSQAwRgJBAMP598ybxxasAER0v8TXk2is+RY7D41g\nY5AttoJFtdBSaD2aRplzu2w+SaM3k/ePBDK8szzzUDAcprh7UV5uHwkCAQM=\n-----END PUBLIC KEY-----\n",
                pvtKey: "-----BEGIN PRIVATE KEY-----\nMIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEAw/n3zJvHFqwARHS/\nxNeTaKz5FjsPjWBjkC22gkW10FJoPZpGmXO7bD5JozeT948EMryzPPNQMBymuHtR\nXm4fCQIBAwJBAIKmpTMShLnIAC2jKoM6YkXIpg7SCl5AQmAeeawueTWLGV3ZCgUi\npSYcRlz5pAivqOF1I20vKMWEMig/sWODuIsCIQD3aQ3GR/dakifzJ2buADVeW6+T\nZ21nZsdbZF843wxNRQIhAMrHxvFJyGkg6+zwWi/qUiiE3WqxvyuhDwAXvI5qHDz1\nAiEApPCz2YVPkbbFTMTvSVV46ZJ1DO+eRO8vkkLqJeoIM4MCIQCHL9n2MTBGFfKd\n9ZF1RuFwWJOcdn9ya19VZShe8WgoowIhANszYqJ3DPwIqHW2/umS4QRX+LmqnoUK\nZcUz254pkJas\n-----END PRIVATE KEY-----\n"
            },
            {
                fingerprint: "b1:92:b2:8e:f6:e2:ab:64:29:5b:20:d3:5c:c0:f4:35:b2:9d:6d:69:f6:8b:4c:fb:47:34:17:9b:b2:00:d0:06",
                pubKey: "-----BEGIN PUBLIC KEY-----\nMFowDQYJKoZIhvcNAQEBBQADSQAwRgJBAOjBnFap/cZB4lrIwLsy4gHozoTHHjtM\n3KM4i4vZiHZPxhs+TDLjlWDS/6+biwIZe5gu8tMNJtc5mfvWpQ/f6JsCAQM=\n-----END PUBLIC KEY-----\n"
            },
            {
                fingerprint: "2f:d1:8f:ed:8f:a7:ca:e2:50:90:d9:e2:86:06:35:d2:81:cb:87:72:bb:2f:f3:44:5a:59:60:c0:c8:86:9d:7e",
                pubKey: "-----BEGIN PUBLIC KEY-----\nMFowDQYJKoZIhvcNAQEBBQADSQAwRgJBALw/kW9IO9GBqwvTPHFYxCmKXpdvbGMP\nGYBZ0Gp8H9eP/H8yk3Ue+rbOYN7dXhDZey/GzomPbFrrsBA9H7o4VssCAQM=\n-----END PUBLIC KEY-----\n"
            },
            {
                fingerprint: "7b:ae:11:ea:4b:83:a1:d0:56:62:73:dc:74:f0:14:6e:33:8a:3b:0b:53:85:b7:08:16:62:cf:b3:4f:56:d5:ed",
                pubKey: "-----BEGIN PUBLIC KEY-----\nMFowDQYJKoZIhvcNAQEBBQADSQAwRgJBANa3oWtCWZncg4OaXbvwMmm0g+q97b2r\niN8aoAurHtxN6+AkvnySWGlLqpRrEnTETohDJfHJfGCRcH+drdvja48CAQM=\n-----END PUBLIC KEY-----\n"
            },
        ],
    },
    getters: {
        numContactsCards: state => state.contactsCards ? state.contactsCards.length : 0,
        numKeys: state => state.ssiKeystore ? state.ssiKeystore.length : 0,
        numWalletCards: state => state.walletCards ? state.walletCards.length : 0,
        getCardFromFingerprint: state => fingerprint =>
            state.contactsCards
            ? state.contactsCards.filter(x => x.fingerprint === fingerprint)
            : [],
        getKeyFromFingerprint: state => fingerprint =>
            state.ssiKeystore
            ? state.ssiKeystore.filter(x => x.fingerprint === fingerprint)
            : [],
        getState: state => state,
    },
    mutations: {
        replaceStateFromBlob(state, blob) {
            try {
                //TODO: validate the json schema
                //console.log(blob)
                const notablob = JSON.parse(blob)
                if (notablob) {
                    this.replaceState(notablob)
                }
            } catch (exception){
                console.error(exception)
            }
        },
        addContactsCardFromBlob({ contactsCards }, blob) {
            const card = JSON.parse(blob)
            if (this.state === undefined) {
                this.replaceState({
                    walletCards:[],
                    contactsCards:[ card ],
                    ssiKeystore:[],
                })
            } else if (contactsCards === undefined) {
                this.replaceState({
                    walletCards: this.state.walletCards,
                    contactsCards: [ card ],
                    ssiKeystore: this.state.ssiKeystore,
                })
            } else {
                contactsCards.push(card)
            }
        },
        addContactsCard({ contactsCards }, card) {
            if (this.state === undefined) {
                this.replaceState({
                    walletCards:[],
                    contactsCards:[ card ],
                    ssiKeystore:[],
                })
            } else if (contactsCards === undefined) {
                this.replaceState({
                    walletCards: this.state.walletCards,
                    contactsCards: [ card ],
                    ssiKeystore: this.state.ssiKeystore,
                })
            } else {
                contactsCards.push(card)
            }
        },
        rmContactsCard({ contactsCards }, index) {
            if (this.state === undefined) {
                this.replaceState({
                    walletCards:[],
                    contactsCards:[],
                    ssiKeystore:[],
                })
            } else if (contactsCards === undefined) {
                this.replaceState({
                    walletCards: this.state.walletCards,
                    contactsCards: [],
                    ssiKeystore: this.state.ssiKeystore,
                })
            } else {
                contactsCards.splice(index, 1)
            }
        },
        addWalletCard({ walletCards }, card) {
            if (this.state === undefined) {
                this.replaceState({
                    walletCards:[ card ],
                    contactsCards:[],
                    ssiKeystore:[],
                })
            } else if (walletCards === undefined) {
                this.replaceState({
                    walletCards:[ card ],
                    contactsCards: this.state.contactsCards,
                    ssiKeystore: this.state.ssiKeystore,
                })
            } else {
                walletCards.push(card)
            }
        },
        rmWalletCard({ walletCards }, index) {
            if (this.state === undefined) {
                this.replaceState({
                    walletCards:[],
                    contactsCards:[],
                    ssiKeystore:[],
                })
            } else if (walletCards === undefined) {
                this.replaceState({
                    walletCards:[],
                    contactsCards: this.state.contactsCards,
                    ssiKeystore: this.state.ssiKeystore,
                })
            } else {
                walletCards.splice(index, 1)
            }
        },
        createKeypair({ ssiKeystore }, instance) {
            if (this.state === undefined) {
                this.replaceState({
                    walletCards: [],
                    contactsCards: [],
                    ssiKeystore: [instance],
                })
            } else if (ssiKeystore === undefined) {
                this.replaceState({
                    walletCards: this.state.walletCards,
                    contactsCards: this.state.contactsCards,
                    ssiKeystore: [instance],
                })
            } else {
                ssiKeystore.push(instance)
            }
        },
    },
    actions: {
        addKeypair: function(context, instance) {
            context.commit("createKeypair", instance)
        },
        addContact(context, instance) {
            context.commit("addContactsCard", instance)
        },
        rmCard(context, index) {
            context.commit("rmContactsCard", index)
        },
        rmCred(context, index) {
            context.commit("rmWalletCard", index)
        }
    }
})
